<?php

App::uses('AdminController', 'Controller');

class AdminCeneController extends AdminController {
    
    public $uses = array('');
    
    public $vrsteVozila = array(
        'putnicko'  => 'Putnička vozila',
        'motorcikl' => 'Motorcikli',
        'teretno'   => 'Teretna vozila'
    );
    
    public $autoRender = false;
    public $layout     = false;
    
    private function _loadData($modelName, $defaultVal) {
        $this->loadModel($modelName);
        
        $data = array();
        foreach ($this->vrsteVozila as $k=>$v) {
            if (empty($data[$k])) $data[$k] = array();
            
            $tmp = $this->$modelName->findAllByVrstaVozila($k);
            if (empty($tmp)) {
                $tmp = array(array( $modelName => $defaultVal));
            }
            $data[$k] = $tmp;
        }
        
        return $data;
    }
    
    private function _setCommonData() {
        $lblVrsteVozila = $this->vrsteVozila;
        $this->set(compact('lblVrsteVozila'));
    }
    
    public function index() {
        // osiguranje
        $data = $this->_loadData('KalkulatorOsiguranje', array(
            'min' => '',
            'max' => '',
            'value' => ''
        ));
        
        $this->_setCommonData();
        $this->set('units', array(
            'putnicko'  => 'kW',
            'motorcikl' => 'ccm',
            'teretno'   => 't'
        ));
        $this->set('data', $data);
        $this->layout = 'admin';
        $this->render('index');
    }
    
    public function index_komunalna() {
        $data = $this->_loadData('KalkulatorKomunalna', array(
            'min'   => '',
            'max'   => '',
            'value' => ''
        ));
        
        $this->_setCommonData();
        $this->set('units', array(
            'putnicko'  => 'ccm',
            'motorcikl' => 'ccm',
            'teretno'   => 't'
        ));
        $this->set('data', $data);
        $this->render('form_komunalna');
    }
    public function index_porez() {
        unset($this->vrsteVozila['teretno']);
        
        $data = $this->_loadData('KalkulatorPorez', array(
            'god_od' => '',
            'god_do' => '',
            'min'    => '',
            'max'    => '',
            'value'  => ''
        ));
        
        $this->_setCommonData();
        $this->set('units', array(
            'putnicko'  => 'ccm',
            'motorcikl' => 'ccm'
        ));
        $this->set('data', $data);
        $this->render('form_porez');
    }
    public function index_nalepnica() {
        $this->loadModel('KalkulatorNalepnica');
        $data = $this->KalkulatorNalepnica->find('all', array(
            'conditions' => array(
                'vrsta_takse' => 'nalepnica'
            )
        ));
        
        $this->set('data', $data);
        $this->render('form_nalepnica');
    }
    public function index_saobracajna() {
        $this->loadModel('KalkulatorSaobracajna');
        $data = $this->KalkulatorSaobracajna->find('all', array(
            'conditions' => array(
                'vrsta_takse' => 'saobracajna'
            )
        ));
        
        $this->set('data', $data);
        $this->render('form_saobracajna');
    }
    public function index_tablice() {
        $this->loadModel('KalkulatorTablice');
        $data = $this->KalkulatorTablice->find('all', array(
            'conditions' => array(
                'vrsta_takse' => 'tablice'
            )
        ));
        
        $this->set('data', $data);
        $this->render('form_tablice');
    }
    
/**
 * 
 *
 * =============  SAVE ===============================================
 * 
 */

    private function _saveData($modelName, $verify, $data=null, $deleteConditions=null) {
        if (! $data) {
            $data = array_merge(@$this->data['putnicko'], @$this->data['teretno'], @$this->data['motorcikl']);
        }
        
        if (! $deleteConditions) {
            $deleteConditions = array(1=>1);
        }
        
        $message = 'Morate uneti neku cenu!';
        if (! empty($data)) {
            $ok = array();
            
            foreach ($data as $row) {
                if ($verify($row)) {
                    $ok[] = $row;
                }
            }

            if (! empty($ok)) {
                $this->loadModel($modelName);
                $this->$modelName->deleteAll($deleteConditions);
                if ($this->$modelName->saveAll($ok)) {
                    $message = 'Cene su uspešno sačuvane';
                } else {
                    $message = 'Greška pri čuvanju cena.';
                }
            }
        }
        return $message;
    }
    
    public function save_osiguranje() {
        echo $this->_saveData('KalkulatorOsiguranje', function($row){
            return !empty($row['KalkulatorOsiguranje']['min']) ||
                   !empty($row['KalkulatorOsiguranje']['max']) ||
                   !empty($row['KalkulatorOsiguranje']['cena']);
        });
    }
    
    public function save_komunalna() {
        echo $this->_saveData('KalkulatorKomunalna', function($row){
            return !empty($row['KalkulatorKomunalna']['min']) ||
                   !empty($row['KalkulatorKomunalna']['max']) ||
                   !empty($row['KalkulatorKomunalna']['cena']);
        });
    }
    public function save_porez() {
        echo $this->_saveData('KalkulatorPorez', function($row){
            return !empty($row['KalkulatorPorez']['god_od']) ||
                   !empty($row['KalkulatorPorez']['god_do']) ||
                   !empty($row['KalkulatorPorez']['min']) ||
                   !empty($row['KalkulatorPorez']['max']) ||
                   !empty($row['KalkulatorPorez']['cena']);
        }, array_merge($this->data['putnicko'], $this->data['motorcikl']));
    }
    
    public function save_nalepnica() {
        echo $this->_saveData('KalkulatorNalepnica', function($row){
            return !empty($row['KalkulatorNalepnica']['company']) ||
                   !empty($row['KalkulatorNalepnica']['value']);
        }, $this->data, array(
            'vrsta_takse' => 'nalepnica'
        ));
    }

    public function save_saobracajna() {
        echo $this->_saveData('KalkulatorSaobracajna', function($row){
            return !empty($row['KalkulatorSaobracajna']['company']) ||
                   !empty($row['KalkulatorSaobracajna']['value']);
        }, $this->data, array(
            'vrsta_takse' => 'saobracajna'
        ));
    }
    
    public function save_tablice() {
        echo $this->_saveData('KalkulatorTablice', function($row){
            return !empty($row['KalkulatorTablice']['company']) ||
                   !empty($row['KalkulatorTablice']['value']);
        }, $this->data, array(
            'vrsta_takse' => 'tablice'
        ));
    }
}
